﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins.AssetManagement;
using System.Web;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.Plugins
{
    public class SafePlugin : IPlugin
    {
        private IPlugin plugin;
        private List<PluginEvent> events = new List<PluginEvent>();

        public SafePlugin(Type type, IPlugin plugin)
        {
            this.plugin = plugin;
            this.Type = type;

            this.events.Add(
                new PluginEvent()
                {
                    Occured = DateTime.Now,
                    Type = PluginEventType.Message,
                    Lifecycle = "Initialization",
                    Message = "Plugin Loaded"
                });
        }

        public Exception Exception
        {
            get;
            internal set;
        }

        public Type Type
        {
            get;
            private set;
        }
       

        public void RegisterException(string lifeCycle, System.Exception ex, TmlExtension tmlExtension, IdeExtension ideExtension)
        {
            this.Exception = ex;
            //this.plugin = null;

            this.events.Add(
               new PluginEvent()
               {
                   Occured = DateTime.Now,
                   Type = PluginEventType.Error,
                   Lifecycle = lifeCycle,
                   Message = ex.Message,
                   TmlExtension = tmlExtension,
                   IdeExtension = ideExtension
               });
        }

        public void RegisterException(System.Exception ex)
        {
            this.RegisterException("Unknown (Unhandled Exception", ex, null, null);
        }

      

        public string Name
        {
            get
            {
                try
                {
                    if (this.plugin != null)
                        return this.plugin.Name;
                }
                catch (Exception ex)
                {
                    this.RegisterException(ex);
                }
                return null;
            }
        }

        public Guid Id
        {
            get
            {
                try
                {
                    if (this.plugin != null)
                        return this.plugin.Id;
                }
                catch (Exception ex)
                {
                    this.RegisterException(ex);
                }
                return Guid.Empty;
            }
        }

        public string Description
        {
            get
            {
                try
                {
                    if (this.plugin != null)
                        return this.plugin.Description;
                }
                catch (Exception ex)
                {
                    this.RegisterException(ex);
                }
                return null;
            }
        }

        public bool IsSafe
        {
            get { return this.plugin != null; }
        }

        public List<PluginEvent> Events
        {
            get { return this.events; }
        }

        public bool Is<T>() 
            where T : IPlugin
        {
            return this.plugin is T;
        }

        public TResult Invoke<T, TResult>(Func<T, TResult> del)
            where T : IPlugin
        {
            try
            {
                if (this.plugin != null)
                    return del((T)this.plugin);
            }
            catch (Exception ex)
            {
                this.RegisterException(ex);
            }
            return default(TResult);
        }


        public void SetProvider(ICmsDataProvider provider)
        {
            this.plugin.SetProvider(provider);
        }
    }
}
